package com.reremouse.service;

import com.reremouse.rerebbs.dao.ISystemConfigDAO;
import com.reremouse.util.SpringHelper;
import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletContext;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageService
        implements Serializable {
    public int MAX_IMAGE_WIDTH = 960;
    private String relativeDir = (String) SpringHelper.getSpringBean("relativeDir");

    public void deleteRelativeImage(ServletContext sc, Long id) {
        String path = sc.getRealPath(this.relativeDir);
        path = path + "\\" + id + ".jpg";
        File ff1 = new File(path);
        if (ff1.exists()) {
            ff1.delete();
        }
    }

    public void transferAllUserImages(ServletContext sc) {
        StringBuilder dir = new StringBuilder();
        dir.append(loadOriginalFilePath(sc));

        dir.append("user_images\\");
        String nfn = dir.toString();

        File fff = new File(nfn);
        String[] fs = fff.list();
        if (fs != null) {
            for (String s : fs) {
                System.out.println(s);
                transferUserImagesToRelativeDir(sc, Long.valueOf(Long.parseLong(s)));
            }
        }
    }

    public void transferToRelativeDir(ServletContext sc, Long id) {
        try {
            Image src = null;
            BufferedImage image = null;
            BufferedImage bimage = null;
            InputStream is = null;
            OutputStream out = null;


            StringBuilder dir = new StringBuilder();
            dir.append(loadOriginalFilePath(sc));
            dir.append(id);
            String nfn = dir.toString();

            File fff = new File(nfn);
            if (!fff.exists()) {
                return;
            }
            is = new BufferedInputStream(new FileInputStream(fff));


            String path = sc.getRealPath(this.relativeDir);
            path = path + "\\" + id + ".jpg";
            File ff1 = new File(path);
            if (ff1.exists()) {
                return;
            }
            out = new BufferedOutputStream(new FileOutputStream(path));

            src = ImageIO.read(is);
            int width = src.getWidth(null);
            int height = src.getHeight(null);
            image = new BufferedImage(width, height, 1);

            Graphics2D g = image.createGraphics();
            image.getGraphics().drawImage(src, 0, 0, null);
            g.drawImage(src, 0, 0, null);
            g.dispose();
            g = null;


            bimage = getResizePicture(image, this.MAX_IMAGE_WIDTH);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
            ImageIO.write(bimage, "jpg", imOut);

            InputStream bis = new ByteArrayInputStream(bos.toByteArray());

            bos.close();
            IOUtils.copy(bis, out);
            bis.close();
            bos = null;
            imOut = null;
            bis = null;
            is.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void transferUserImagesToRelativeDir(ServletContext sc, Long id) {
        try {
            Image src = null;
            BufferedImage image = null;
            BufferedImage bimage = null;
            InputStream is = null;
            OutputStream out = null;


            StringBuilder dir = new StringBuilder();
            dir.append(loadOriginalFilePath(sc));
            dir.append("user_images\\");
            dir.append(id);
            String nfn = dir.toString();

            File fff = new File(nfn);
            if (!fff.exists()) {
                return;
            }
            is = new BufferedInputStream(new FileInputStream(fff));


            String path = sc.getRealPath(this.relativeDir);
            path = path + "\\" + id + ".jpg";
            File ff1 = new File(path);
            if (ff1.exists()) {
                return;
            }
            out = new BufferedOutputStream(new FileOutputStream(path));

            src = ImageIO.read(is);
            int width = src.getWidth(null);
            int height = src.getHeight(null);
            image = new BufferedImage(width, height, 1);

            Graphics2D g = image.createGraphics();
            image.getGraphics().drawImage(src, 0, 0, null);
            g.drawImage(src, 0, 0, null);
            g.dispose();
            g = null;


            bimage = getResizePicture(image, this.MAX_IMAGE_WIDTH);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
            ImageIO.write(bimage, "jpg", imOut);

            InputStream bis = new ByteArrayInputStream(bos.toByteArray());

            bos.close();
            IOUtils.copy(bis, out);
            bis.close();
            bos = null;
            imOut = null;
            bis = null;
            is.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private final BufferedImage getResizePicture(BufferedImage originalPic, int w) {
        int originalImageWidth = originalPic.getWidth();

        int originalImageHeight = originalPic.getHeight();
        if (originalImageWidth < w) {
            return originalPic;
        }
        double bo = w / originalImageWidth;


        int changedImageWidth = w;

        int changedImageHeight = (int) (originalImageHeight * bo);

        BufferedImage changedImage = new BufferedImage(changedImageWidth, changedImageHeight, originalPic.getType());

        changedImage.getGraphics().drawImage(originalPic, 0, 0, changedImageWidth, changedImageHeight, null);
        return changedImage;
    }

    private String loadOriginalFilePath(ServletContext sc) {
        String dir = "";
        ISystemConfigDAO scd = (ISystemConfigDAO) SpringHelper.getSpringBean("SystemConfigDAO");
        boolean b = scd.getSystemConfig().getIfRelative().booleanValue();
        if (b) {
            String tp = scd.getSystemConfig().getFilePath();
            if (!tp.endsWith("/")) {
                tp = tp + "/";
            }
            dir = sc.getRealPath(tp);
        } else {
            dir = scd.getSystemConfig().getFilePath();
        }
        if (!dir.endsWith("\\")) {
            dir = dir + "\\";
        }
        return dir;
    }

    public static void main(String[] args) {
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.service.ImageService

 * JD-Core Version:    0.7.0.1

 */